package com.maya.controller;

import javax.servlet.http.HttpSession;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.maya.model.Answer;
import com.maya.model.User;
import com.maya.service.AnswerService;

@Controller

public class AnswerVote {
	
	@Autowired
	private AnswerService answerService;
	
	@ResponseBody
	@RequestMapping("/answervote")
	public String answervote(String answerId,String vote,HttpSession session){
		
		User user = (User)session.getAttribute("currUser");
		Answer answer = answerService.get(new ObjectId(answerId));
		if(user==null) return Integer.toString(answer.getVoteCount());
		if(vote.equals("up")){
			answer.incVoteCount();
			
		}else if(vote.equals("down")){
			answer.decVoteCount();
		}
		answerService.save(answer);
		return Integer.toString(answer.getVoteCount());
	}

}
